/**
 * 匿名方法，保护变量作用域
 */
(function() {
    var ajax_url = __ajax_url;
    var permission_all_id = __permission_all_id;
    var perission_self_id = __perission_self_id;

    var permission_alllist = new helper.base.Map();// 所有权限
    var permission_rootlist = new helper.base.Map();// 全部根权限对象,层次存放,使用Map存放
    var permission_self = new helper.base.Map();// 已经存在的权限对象，层次存放，使用Map存放

    $.ajax({
        type : "post",
        url : ajax_url,
        dataType : "json",
        success : function(date, status) {
            /** 已经存在的权限 */
            var __permission_self = date.selflist;
            /** 所有顶级权限 */
            var __permission_rootlist = date.rootlist;
            /** 所有权限 */
            var __permission_alllist = date.alllist;

            if (__permission_self != null
                    && __permission_self.length > 0) {
                for (var i = 0; i < __permission_self.length; i++) {
                    permission_self.put(__permission_self[i].id,
                            __permission_self[i]);
                }
            }
            for (var i = 0; i < __permission_rootlist.length; i++) {
                permission_rootlist.put(__permission_rootlist[i].id,
                        __permission_rootlist[i]);
            }
            for (var i = 0; i < __permission_alllist.length; i++) {
                permission_alllist.put(__permission_alllist[i].id,
                        __permission_alllist[i]);
            }

            // 显示所有权限
            showAllPermissionColor();
            if (permission_self.getMap().length > 0) {
                showSelf(permission_self, perission_self_id);
            }
        },
        error : function() {
            alert("ajax请求出现异常.");
        }
    });

    function digui(childs, selectBoxId) {
        if (childs != null) {
            for (var i = 0; i < childs.length; i++) {
                var _permission = childs[i];
                var space = "";
                while (_permission.parentId != null) {
                    space += "&nbsp;&nbsp;&nbsp;&nbsp;";
                    _permission = permission_alllist.get(_permission.parentId);
                }
                var __current_box = document.getElementById(selectBoxId);
                helper.fun.addItems(__current_box, childs[i].id,
                        space + childs[i].description);
                //递归显示子级权限
                digui(childs[i].child, selectBoxId);
            }
        }
    }

    /**
     * 显示权限，分层次显示
     *
     * @param permissions
     *            权限的MAP
     * @param selectBoxId
     *            要显示的控件ID
     */
    function showSelf(permissions, selectBoxId) {
        var __current_box = document.getElementById(selectBoxId);
        helper.fun.removeAllItems(__current_box);
        for (var i = 0; i < permissions.getMap().length; i++) {
            var __permission = permissions.getMap()[i].value;
            if (__permission.parentId == null) {
                helper.fun.addItems(__current_box, __permission.id,
                        __permission.description);
                /* 根权限下的子权限 */
                var actions = __permission.child;
                digui(actions, selectBoxId);
            }
        }
    }

    function joinPermission(map, permission) {
        map.put(permission.id, permission);
        var _child = permission.child;
        if (_child != null) {
            for (var i = 0; i < _child.length; i++) {
                joinPermission(map, _child[i]);
            }
        }
    }


    function diguiShowAll(childs, selectBoxId, _list) {
        if (childs != null) {
            for (var i = 0; i < childs.length; i++) {
                var _permission = childs[i];
                var space = "";
                while (_permission.parentId != null) {
                    space += "&nbsp;&nbsp;&nbsp;&nbsp;";
                    _permission = permission_alllist.get(_permission.parentId);
                }
                var __current_box = document.getElementById(selectBoxId);
                if (_list.get(childs[i].id) != null) {
                    helper.fun.addItems(__current_box, childs[i].id,
                            space + childs[i].description, "#CCCCCC");
                } else {
                    helper.fun.addItems(__current_box, childs[i].id,
                            space + childs[i].description);
                }
                //递归显示子级权限
                diguiShowAll(childs[i].child, selectBoxId, _list);
            }
        }
    }


    /**
     * 显示权限，分层次显示,控制选中颜色
     *
     * @param permissions
     *            权限的MAP
     * @param selectBoxId
     *            要显示的控件ID
     */
    function showAllPermissionColor() {
        var __current_box = document.getElementById(permission_all_id);
        helper.fun.removeAllItems(__current_box);
        // 所有的权限，层次
        var permissions = permission_rootlist;
        var child_permissions = permission_self;
        //临时存放已经存在权限列表，不分层显示
        var _list = new helper.base.Map();
        for (var z = 0; z < child_permissions.getMap().length; z++) {
            var __permission = child_permissions.getMap()[z].value;
            joinPermission(_list, __permission);
        }
        for (var i = 0; i < permissions.getMap().length; i++) {
            var __permission = permissions.getMap()[i].value;
            if (__permission.parentId == null) {
            	if (_list.get(__permission.id) != null) {
            		 helper.fun.addItems(__current_box, __permission.id,
                             __permission.description, "#CCCCCC");
            	}else {
            		 helper.fun.addItems(__current_box, __permission.id,
                             __permission.description);
            	}
                /* 根权限下的子权限 */
                var actions = __permission.child;
                diguiShowAll(actions, permission_all_id, _list);
            }
        }
    }


    function addPermissions(permission) {
        var __family = new Array();
        __family.unshift(permission);
        while (permission.parentId != null) {
            var _permission = permission_alllist.get(permission.parentId);
            __family.unshift(_permission);
            permission = _permission;
        }
        var _temp = null;
        AA:
                for (var i = 0; i < __family.length; i++) {
                    var _permission = __family[i];
                    if (_temp == null) {
                        if (permission_self.get(_permission.id) == null) {
                            var _per = {
                                id : _permission.id,
                                description : _permission.description,
                                parentId : null,
                                child : null
                            };
                            permission_self.put(_per.id, _per);
                            _temp = _per;
                        } else {
                            _temp = permission_self.get(_permission.id);
                        }
                    } else {
                        var __child = _temp.child;
                        if (__child != null) {
                            for (var j = 0; j < __child.length; j++) {
                                var _p = __child[j];
                                if (_p.id == _permission.id) {
                                    _temp = _p;
                                    continue AA;
                                }
                            }
                            var _per = {
                                id : _permission.id,
                                description : _permission.description,
                                parentId : _temp.id,
                                child : null
                            };
                            _temp = _per;
                            __child.push(_per);
                        } else {
                            var _per = {
                                id : _permission.id,
                                description : _permission.description,
                                parentId : _temp.id,
                                child : null
                            };
                            _temp.child = [_per];
                            _temp = _per;
                        }
                    }
                }
    }

    function removePermissions(permission) {
        var _per = permission;
        var __family = new Array();
        while (permission.parentId != null) {
            var _permission = permission_alllist.get(permission.parentId);
            __family.unshift(_permission);
            permission = _permission;
        }
        var _temp = null;
        for (var i = 0; i < __family.length; i++) {
            var _permission = __family[i];
            if (_temp == null) {
                _temp = permission_self.get(_permission.id);
            } else {
                var _child = _temp.child;
                for (var j = 0; j < _child.length; j++) {
                    if (_child[j].id == _permission.id) {
                        _temp = _child[j];
                    }
                }
            }
        }
        if (_temp) {
            var __child = _temp.child;
            for (var z = 0; z < __child.length; z++) {
                var v = __child.pop();
                if (v.id == _per.id) {
                    continue;
                }
                __child.unshift(v);
            }
        }
    }

    function add() {
        /* 权限列表box */
        var __selectBox = document.getElementById(__permission_all_id);
        if (__selectBox.selectedIndex == -1) {
            alert("请选择您要操作的数据！");
            return;
        }
        OPTION: for (var i = 0; i < __selectBox.options.length; i++) {
            if (__selectBox.options[i].selected) {
                var value = __selectBox.options[i].value;
                var per = permission_alllist.get(value);// 根据权限ID，得到权限对象
                addPermissions(per);
            }
        }
        showAllPermissionColor();
        showSelf(permission_self, perission_self_id);
    }

    function remove() {
        /* 权限列表box */
        var __selectBox = document.getElementById(perission_self_id);
        if (__selectBox.selectedIndex == -1) {
            alert("请选择您移除的权限！");
            return;
        }
        var _list = new helper.base.Map();
        for (var z = 0; z < permission_self.getMap().length; z++) {
            var __permission = permission_self.getMap()[z].value;
            joinPermission(_list, __permission);
        }
        for (var i = 0; i < __selectBox.options.length; i++) {
            if (__selectBox.options[i].selected) {
                var value = __selectBox.options[i].value;
                var per = permission_alllist.get(value);// 根据权限ID，得到权限对象
                if (per.parentId == null) {
                    permission_self.remove(per.id);
                } else {
                    removePermissions(per);
                }
            }
        }
        showAllPermissionColor();
        showSelf(permission_self, perission_self_id);
    }

    window["appRoleCreate"] = {};
    window["appRoleCreate"]["addPermission"] = add;
    window["appRoleCreate"]["removePermission"] = remove;
})();